﻿using Microsoft.EntityFrameworkCore;
using MiniFox.Data.Configurations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Data.EntityFramework
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class DbContextBase : DbContext
    {
        ConnectionConfiguration _connectionConfig;
        int _commandTimeout;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="options"></param>
        public DbContextBase(string connectionName, DbContextOptions options) : this(connectionName, 30, options)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="commandTimeout"></param>
        /// <param name="options"></param>
        public DbContextBase(string connectionName, int commandTimeout, DbContextOptions options) : base(options)
        {
            _connectionConfig = Data.Database.Configuration.Connections[connectionName];
            _commandTimeout = commandTimeout;
        }
        /// <summary>
        /// 
        /// </summary>
        public string ConnectionName
        {
            get { return _connectionConfig.Name; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return _connectionConfig.ConnectionString;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ProviderName
        {
            get
            {
                return _connectionConfig.ProviderName;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int CommandTimeout
        {
            get
            {
                return _commandTimeout;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder);
    }
}
